import { fileURLToPath, URL } from "node:url"

import { defineConfig } from "vite"
import vue from "@vitejs/plugin-vue"
import vueJsx from "@vitejs/plugin-vue-jsx"
import tailwindcss from "@tailwindcss/vite"
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from "unplugin-vue-components/resolvers"
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';

const pkg = JSON.parse(fs.readFileSync(path.resolve(__dirname, './package.json'), 'utf-8'));
const version = pkg.version.replace(/\./g, '_') + '_' + new Date().toLocaleString().replace(/\//g, '_').replace(/:/g, '_').replace(" ", "_");

dotenv.config();

export default defineConfig({
	server:{
		host: '0.0.0.0',
		proxy:{
			'/api':{
				target: process.env.VITE_API_BASE_URL,
				changeOrigin:true,
				rewrite:path=>path.replace(/^\/api/,'')
			}
		}
	},
	build: {
		outDir: `dist_${version}`
	},
	css: {
		preprocessorOptions: {
			scss: {
				additionalData: `@use "@/styles/element/index.scss" as *;`,
			},
		},
	},
	plugins: [
		vue(),
		vueJsx(),
		tailwindcss(),
		AutoImport({
			resolvers: [ElementPlusResolver()],
		}),
		Components({
			// resolvers: [ElementPlusResolver()],
			resolvers: [ElementPlusResolver({importStyle:"sass"})],
		}),
	],
	resolve: {
		alias: {
			"@": fileURLToPath(new URL("./src", import.meta.url)),
		},
	},
})
